package com.jlastu.znb.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

   private static final String TAG = "DBHelper";
   private static final String DATABASE_NAME = "znb.db";// 数据库名
   private static final int DATABASE_VERSION = 2;// 数据库版本号
   private static final String TABLE_NAME_GOODS = "goods"; //商品表
   private static final String TABLE_NAME_USER = "user"; //用户表
   private static final String TABLE_NAME_ORDER = "order2"; // 订单表 order是关键字
   private static final String TABLE_NAME_SETTINGS = "settings"; // 设置表
   private static final String TABLE_NAME_PHOTO = "photo";
   private static final String TABLE_NAME_NEWS = "news";
   private static final String TABLE_NAME_NEWSPHOTO = "newsphoto";

   public DBHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }

   //数据库创建时被调用
   @Override
   public void onCreate(SQLiteDatabase db) {
      initTable(db);
   }

   //数据库升级
   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      if (oldVersion < 2) { // 数据库 1 -> 2 升级
         // 创建设置表
         db.execSQL("delete from " + TABLE_NAME_GOODS);
      }
   }

   /**
    * 初始化数据表
    *
    * @param db 数据库
    */
   private void initTable(SQLiteDatabase db) {
      // 创建商品表
      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_GOODS + " (" +
              "_id integer primary key autoincrement," +
              "goods_name text," +
              "goods_price double," +
              "goods_unit text," +
              "goods_browse integer," +
              "goods_message text," +
              "goods_address text," +
              "category text," +
              "shippingAddress text," +
              "is_collect boolean DEFAULT 0" + // 新增字段，布尔类型，默认值为0（表示未收藏）
              ")");
      // 创建用户表
      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_USER + " (" +
              "_id integer primary key autoincrement," +
              "user_name text," +
              "user_password text)");
      // 创建订单表
      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_ORDER + " (" +
              "_id integer primary key autoincrement," +
              "order_name text," +
              "order_number  bigint," +
              "user_id  integer," +
              "FOREIGN KEY (user_id) REFERENCES user(_id))");
      // 创建图片表
      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_PHOTO + " (" +
              "_id integer primary key autoincrement," +
              "goods_id  integer," +
              "image BLOB," +
              "FOREIGN KEY (goods_id) REFERENCES goods(_id))");

      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_NEWS + " (" +
              "_id integer primary key autoincrement," +
              "title text," +
              "subtitle text," +
              "date text," +
              "content text," +
              "category text" +
              ");");

      db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_NEWSPHOTO + " (" +
              "_id integer primary key autoincrement," +
              "news_id  integer," +
              "image BLOB," +
              "FOREIGN KEY (news_id) REFERENCES news(_id))");

   }

   /**
    * 初始化数据库内部模拟数据(仅在第一次安装时执行)
    *
    * @param db 数据库
    */

   /*private void initData(SQLiteDatabase db) {
      db.execSQL("insert into " + TABLE_NAME_GOODS +
              " (goods_name , goods_price, goods_unit, goods_browse, goods_message) " +
              "values" +
              "('四川峨眉山黄瓜', 1.23, '斤', '123', '来自四川雪山之巅的黄瓜。')");
      db.execSQL("insert into " + TABLE_NAME_GOODS +
              " (goods_name , goods_price, goods_unit, goods_browse, goods_message) " +
              "values" +
              "('东北大米', 3.33, '斤', '444', '来自东北的大米，煮饭特别香。')");
      db.execSQL("insert into " + TABLE_NAME_GOODS +
              " (goods_name , goods_price, goods_unit, goods_browse, goods_message) " +
              "values" +
              "('海南香蕉', 9.99, '斤', '213', '海南香蕉，特别好吃')");
      db.execSQL("insert into " + TABLE_NAME_GOODS +
              " (goods_name , goods_price, goods_unit, goods_browse, goods_message) " +
              "values" +
              "('新疆又香又甜的哈密瓜', 5.99, '斤', '333', '又大又甜，特别好吃')");
      db.execSQL("insert into " + TABLE_NAME_GOODS +
              " (goods_name , goods_price, goods_unit, goods_browse, goods_message) " +
              "values" +
              "('江苏黄瓜', 1.12, '斤', '223', '来自江苏的黄瓜。')");

      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_name , user_password) " +
              "values" +
              "('1', '1')");
      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_name , user_password) " +
              "values" +
              "('2', '2')");
      db.execSQL("insert into " + TABLE_NAME_USER +
              " (user_name , user_password) " +
              "values" +
              "('3', '3')");
   }*/
   public void updateGoodsBrowseCount(int goodsId, int newBrowseCount) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put("goods_browse", newBrowseCount);
      db.update(TABLE_NAME_GOODS, values, "_id = ?", new String[]{String.valueOf(goodsId)});
      db.close();
   }
}
